package m202406.d28;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/28 21:22
 */
public class Q44 {

    public static void main(String[] args) {
        Q44 q44 = new Q44();
        q44.isMatch("aa", "*");
    }


    //dp[i][j] s的前i个字符与p的前j个字符是否匹配
    public boolean isMatch(String s, String p) {
        boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
        //dp[i][0] = false
//        for (int i = 0; i < dp.length; i++) {
//            dp[i][0] = false;
//        }
        //dp[0][j] 当p的前j个字符均为*时为true
        for (int i = 1; i <= p.length(); i++) {
            if (p.charAt(i - 1) != '*') {
                break;
            }
            dp[0][i] = true;
        }
        dp[0][0] = true;

        for (int i = 1; i <= s.length(); i++) {
            for (int j = 1; j <= p.length(); j++) {
                //p(j) 是字符
                char c = p.charAt(j-1);
                if (c >= 'a' && c <= 'z') {
                    dp[i][j] = dp[i - 1][j - 1] && c == s.charAt(i-1);
                } else if (c == '?') {
                    dp[i][j] = dp[i - 1][j - 1];
                } else { //c是*
                    dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                }
            }
        }

        return dp[s.length()][p.length()];
    }
}
